package com.gonsin.gview.general.generator;

import com.gonsin.gview.general.po.EntityPo;
import com.gonsin.gview.general.po.TotalPo;
import com.gonsin.gview.general.utils.FreemarkerUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

import static com.gonsin.gview.general.Main.CONFIG;

/**
 * xxMapper 生成器
 */
public class BeanGenerator implements IGenerator{

    private static final String RELATIVE_PATH = File.separator + "bean";
    private static final String TEMPLATE_PATH = "/java/bean.ftl";
    private static final String OUT_FILE_NAME = "%sBean.java";

    @Override
    public void general(File targetPath, EntityPo po) {
        File outDir = new File(targetPath.getAbsolutePath() + RELATIVE_PATH);
        if(!outDir.exists()){
            outDir.mkdirs();
        }

        File templatePath = new File("templates");

        String content = FreemarkerUtils.format(templatePath, TEMPLATE_PATH, new TotalPo(po, CONFIG));
        File outFile = new File(targetPath.getAbsolutePath() + RELATIVE_PATH + File.separator + String.format(OUT_FILE_NAME, po.getEntityName()));

        try {
            FileUtils.writeStringToFile(outFile, content, CONFIG.getCharset());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
